package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.srevice.OrderDetailService;
import com.itheima.reggie.srevice.OrderService;
import com.itheima.reggie.srevice.UserService;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.lang.model.element.VariableElement;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;



    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("orders:{}",orders);
        orderService.submit(orders);
        return R.success("下单成功！");
    }

    /**
     * 用户查看自己的订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page, int pageSize){
        log.info("userPage-userId:{}",BaseContext.getCurrentId());
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();


        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(Orders::getOrderTime);

        orderService.page(ordersPage,queryWrapper);

        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");

        List<Orders> records = ordersPage.getRecords();
        List<OrdersDto> orderDetailList = records.stream().map(item -> {
            LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
            log.info("item.getNumber:{}",item.getNumber());
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);

            wrapper.eq(OrderDetail::getOrderId,item.getNumber());
            List<OrderDetail> list = orderDetailService.list(wrapper);
            ordersDto.setOrderDetails(list);
            log.info("{}",ordersDto);
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(orderDetailList);

        return R.success(ordersDtoPage);
    }

    /**
     * 查询所有订单
     * @param map
     * @return
     */
    @GetMapping("/page")
    public R<Page<OrdersDto>> list(@RequestParam Map map){
        String page = map.get("page").toString();
        String pageSize = map.get("pageSize").toString();
        String number = map.get("number") !=null ? map.get("number").toString():null;
        String beginTime = map.get("beginTime")!=null ? map.get("beginTime").toString():null;
        String endTime = map.get("endTime")!=null ? map.get("endTime").toString():null;


        log.info("page:{}",page);
        log.info("pageSize:{}",pageSize);
        log.info("number:{}",number);
        log.info("beginData:{}",beginTime);
        log.info("endDate:{}",endTime);


        Page<Orders> ordersPage = new Page<>(Long.parseLong(page),Long.parseLong(pageSize));

        Page<OrdersDto> ordersDtoPage = new Page<>();


        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(beginTime!=null,Orders::getOrderTime,beginTime);
        queryWrapper.lt(endTime!=null,Orders::getOrderTime,endTime);
        queryWrapper.eq(number!=null,Orders::getNumber,number);

        orderService.page(ordersPage,queryWrapper);

        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");

        List<Orders> records = ordersPage.getRecords();
        List<OrdersDto> ordersDtos = records.stream().map(item -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);

            Long userId = item.getUserId();
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(userId != null, User::getId, userId);
            User one = userService.getOne(wrapper);
            ordersDto.setUserName(one.getName());
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(ordersDtos);

        return R.success(ordersDtoPage);
    }

    @PutMapping
    public R<String> status(@RequestBody Orders orders){
        Integer status = orders.getStatus();
        Long id = orders.getId();
        Orders order = orderService.getById(id);
        order.setStatus(status);
        orderService.updateById(order);
        return  R.success("修改成功！");
    }

    @PostMapping("/again")
    public R<OrdersDto> again(){
        return null;
    }

}
